﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace jzgl
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)//返回按钮
        {
            this.Visible = false;
            Login lo = new Login();
            lo.ShowDialog();
        }
        private bool FormatJudge(TextBox t,String s)//判断格式方法
        {
            try { int.Parse(t.Text); return true; }
            catch { MessageBox.Show(s); return false; }
        }
        private void RegisterButton_Click(object sender, EventArgs e)//注册
        {
            DataClasses1DataContext DB = new DataClasses1DataContext();
            if (!string.IsNullOrWhiteSpace(UserNametextBox.Text) && !string.IsNullOrWhiteSpace(PswtextBox.Text) && !string.IsNullOrWhiteSpace(PswtextBox2.Text)
                && !string.IsNullOrWhiteSpace(NametextBox.Text) && !string.IsNullOrWhiteSpace(IDtextBox.Text) && !string.IsNullOrWhiteSpace(LitextBox.Text))
            {
                if (FormatJudge(LitextBox, "驾照格式不正确（8位纯数字）") && FormatJudge(IDtextBox, "身份证格式不正确（8位纯数字）"))
                {
                    Users p1 = DB.Users.SingleOrDefault(p => p.USERNAME.ToString().Trim() == UserNametextBox.Text.ToString().Trim());
                    Users p2 = DB.Users.SingleOrDefault(p => p.LICENSENUM == int.Parse(LitextBox.Text.ToString()));
                    if (p1 != null)
                    {
                        MessageBox.Show("用户名已被使用");
                    }
                    else if (p2 != null) { MessageBox.Show("驾照已被注册"); }
                    else
                    {
                        Users us = new Users()
                        {
                            USERNAME = UserNametextBox.Text.ToString(),
                            PASSWORD = PswtextBox.Text.ToString(),
                            NAME = NametextBox.Text.ToString(),
                            IDNUM = int.Parse(IDtextBox.Text.ToString()),
                            LICENSENUM = int.Parse(LitextBox.Text.ToString()),
                            STATE = 0
                        };
                        DB.Users.InsertOnSubmit(us);
                        DB.SubmitChanges();
                        MessageBox.Show("注册完成，等待管理员审核");
                        Application.Restart();
                    }
                }
            }
            else { MessageBox.Show("请填写完整"); }    
        }
    }
}
